package org.itfarmer.service.impl;

import java.util.List;

import org.itfarmer.dao.MessageDAO;
import org.itfarmer.dao.pojo.Message;
import org.itfarmer.service.MessageService;

public class MessageServiceImpl implements MessageService {

	private MessageDAO messageDAO;
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.BaseService#countService()
	 */
	public Integer countService() {
		Integer rows = messageDAO.count(Message.class);
		return rows;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.MessageService#searchAll(java.lang.Integer, java.lang.Integer)
	 */
	public List<Message> searchAll(Integer pageNow, Integer pageSize) {
		Integer firstRow = (pageNow - 1) * pageSize;
		Integer maxRow = pageSize;
		List list = messageDAO.read(Message.class, firstRow, maxRow);
		return list == null || list.size() ==0 ? null : list;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.MessageService#searcheById(java.lang.Integer)
	 */
	public Message searcheById(Integer mid) {
		if (mid == null) {
			return null;
		}
		Message message = messageDAO.read(Message.class, mid);
		return message;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.MessageService#searchBySender(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<Message> searchBySender(Integer sender, Integer pageNow,
			Integer pageSize) {
		if (sender == null) {
			return null;
		}
		Integer firstRow = (pageNow - 1) * pageSize;
		Integer maxRow = pageSize;
		List<Message> messageList = messageDAO.readBySender(sender, firstRow, maxRow);
		return messageList;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.MessageService#searchByReceiver(java.lang.Integer, java.lang.Integer, java.lang.Integer)
	 */
	public List<Message> searchByReceiver(Integer receiver, Integer pageNow,
			Integer pageSize) {
		if (receiver == null) {
			return null;
		}
		Integer firstRow = (pageNow - 1) * pageSize;
		Integer maxRow = pageSize;
		List<Message> messageList = messageDAO.readBySender(receiver, firstRow, maxRow);
		return messageList;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.MessageService#insertMessage(org.itfarmer.dao.pojo.Message)
	 */
	public boolean insertMessage(Message message) {
		if (message == null) {
			return false;
		}
		Integer mid = messageDAO.create(message);
		return mid == null || mid <= 0 ? false : true;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.MessageService#makeRead(java.lang.Integer)
	 */
	public boolean makeRead(Integer mid) {
		if (mid == null) {
			return false;
		}
		Message message = messageDAO.read(Message.class, mid);
		message.setIsread(1);
		boolean checked = messageDAO.update(message);
		return checked;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.MessageService#countByReceiver(java.lang.Integer)
	 */
	public Integer countByReceiver(Integer receiver) {
		Integer receiverNum = messageDAO.countByReceiver(receiver);
		return receiverNum;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.MessageService#countBySender(java.lang.Integer)
	 */
	public Integer countBySender(Integer sender) {
		Integer senderNum = messageDAO.countBySender(sender);
		return senderNum;
	}

	public MessageDAO getMessageDAO() {
		return messageDAO;
	}

	public void setMessageDAO(MessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}

	
}
 